<?php
include("../models/entities/class.AccountEntity.php");
@session_start();
if(isset($_SESSION["Username"]))
{
include("../models/webconfig.php");
include("../models/class.connection.php");
include("../models/entities/class.AuthorityEntity.php");
include("../models/repositories/class.AuthorityRepository.php");
include("../models/entities/class.GroupEntity.php");
include("../models/repositories/class.GroupRepository.php");

    include("../models/entities/class.GroupAuthorityEntity.php");
    include("../models/repositories/class.GroupAuthorityRepository.php");
    if(isset($_SESSION['Account'])){
        $groupAuthorityRepository = new GroupAuthorityRepository();
        $account = $_SESSION['Account'];
        if(!$groupAuthorityRepository->CheckExistGroupAuthority($account->GroupID_i, 4))
            header("location:NoAccess.php");
    }
    else
    {
        header("location:NoAccess.php");
    }

$authorityRepository = new AuthorityRepository();
$groupRepository = new GroupRepository();
$listAuthority = $authorityRepository->GetAll();
$listgroup = $groupRepository->GetAll();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <link href="../content/Layout.css" media="screen" rel="stylesheet" type="text/css">
        <link href="../content/groupManager.css" rel="stylesheet" type="text/css">
        <script type="text/javascript" src="../scripts/jquery-1.4.2.min.js"></script>
        <link href="../content/ui-lightness/jquery-ui-1.8.6.custom.css" rel="stylesheet" type="text/css" media="screen"/>
        <script type="text/javascript" src="../scripts/jquery-ui-1.8.6.custom.min.js"></script>
        <script type="text/javascript" src="../scripts/admingroup.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {

                var str = location.href.toLowerCase();

                $('ul#mainNav li a').each(function () {
                    if (str.indexOf($(this).attr("href").toLowerCase()) > -1)
                        $(this).addClass("active");
                });
        

                $('ul#sideNav li a').each(function () {
                    if (str.indexOf($(this).attr("href").toLowerCase()) > -1)
                        $(this).addClass("active");
                });

                ManagerScript.init();
            });
        </script>

    </head>
    <body>
        <div id="format">
            <div id="wrapper">
                <!--Phần main menu--><?php
include("../includes/backend_mainnav.php");
?>
                <div id="containerHolder">
                    <div id="container">
                        <div id="maincontent">
                            <h3>Quản lý nhóm người dùng</h3>
                            <div id="content">

                                <form method="post" action="GroupManager.php">
                                    <p class="message" style="color:red; padding-left:20px;">
                                        <?php
                                        if (isset($_POST['update'])) {
                                            if (isset($_POST['check'])) {
                                                if (isset($_POST['groupID'])) {
                                                    $groupID = $_POST['groupID'];
                                                    $list = $_POST['check'];
                                                    if ($authorityRepository->UpdateGroupAu($groupID, $list)) {
                                                        echo "(* Cập nhật quyền thành công!)";
                                                    } else {
                                                        echo "(* Cập nhật không thành công, Vui lòng thử lại!)";
                                                    }
                                                }
                                            } else {
                                                echo "(* Bạn chưa chọn quyền cho nhóm!)";
                                            }
                                        }
                                        ?>
                                    </p>
                                    <h4 class="margin">Chọn nhóm </h4>
                                    <p id="select">
                                        <select>
                                            <?php if (isset($listgroup)) {
                                            ?>
                                            <?php if (count($listgroup) > 0) {
                                            ?>
                                            <?php for ($i = 0; $i < count($listgroup); $i++) {
                                            ?>
                                                        <option id="<?php echo $listgroup[$i]->ID_i ?>"><?php echo $listgroup[$i]->Name_s ?></option>
                                            <?php } ?>
                                            <?php } ?>
                                            <?php } ?>
                                        </select>
                                        <input id="groupID" type="hidden" name="groupID" value=""/>
                                    </p>
                                    <h4>Chức năng tương ứng </h4>
                                    <div id="function">
                                        <div class="loading"></div>
                                        <table width="100%">
                                            <tr class="animate">
                                                <td class="title">STT</td>
                                                <td class="title">Mã chức năng</td>
                                                <td class="title">Tên chức năng </td>
                                                <td class="title">Ghi chú</td>
                                            </tr>
                                            <?php if (isset($listAuthority)) {
                                            ?>
                                            <?php if (count($listAuthority) > 0) {
                                            ?>
                                            <?php
                                                    $list = array();
                                                    if (isset($listgroup[0])) {
                                                        $list = $authorityRepository->GetAuthoritiesByGroup($listgroup[0]->ID_i);
                                                    }
                                                    for ($i = 0; $i < count($listAuthority); $i++) {
                                                        $check = 0;
                                                        if (count($list) > 0) {
                                                            for ($j = 0; $j < count($list); $j++) {
                                                                if ($listAuthority[$i]->ID_i == $list[$j]->ID_i) {
                                                                    $check = 1;
                                                                    break;
                                                                }
                                                            }
                                                        }
                                            ?>
                                                        <tr class="animate">
                                                            <td><?php echo($i + 1); ?></td>
                                                            <td><?php echo($listAuthority[$i]->ID_i); ?></td>
                                                            <td style="text-align:left; padding-left:50px;">
                                                                <label><?php echo($listAuthority[$i]->Name_s); ?></label>
                                                    <?php if ($check == 0) {
                                                    ?>
                                                            <input name="check[]" type="checkbox" value="<?php echo($listAuthority[$i]->ID_i); ?>">
                                                    <?php } else {
 ?>
                                                            <input checked="checked" name="check[]" type="checkbox" value="<?php echo($listAuthority[$i]->ID_i); ?>">
<?php } ?>
                                                    </td>
                                                    <td><?php echo($listAuthority[$i]->Description_s); ?></td>
                                                </tr>
                                            <?php } ?>
                                            <?php } ?>
<?php } ?>
                                        </table>
                                        <p style="font-weight:bold; color:#54A7D8;">Chọn tất cả
                                            <input type="checkbox" name="checkall" id="checkall"></p>
                                    </div>
                                    <p style="margin-top:20px; text-align:center">
                                        <input id="update" name="update" type="submit" value="Cập nhật" style="width: 126px">
                                        <input id="delete" type="button" value="Xóa nhóm" onclick="return false;" style="width: 126px">
                                        <input type="button" value="Thêm nhóm" onclick="window.location.href='CreateGroup.php'" style="width: 126px">
                                    </p>
                                </form>
                                <div id="messagebox" title=" Xác nhận "></div>
                            </div>
                        </div>
                    </div>
                    <!-- // #main --></div>
                <!-- // #container -->
                <!--Phần footer của trang--><?php
                                            include("../includes/backend_footer.php");
?></div>
        </div>
    </body>
</html>
<?php
}
else
{
    header("location:../views/Admin_login.php?logout=TRUE");
}
?>